<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-3-3
 * Time: 下午10:17
 */
//数据统计
namespace Service\Controller;
use Mcit\Page;

class StatisticsController extends BaseController {

    //分类列表
    public function index(){
        //充电站总数
        $location         = session('location');
        $re               = strsToArray($location);
        $map['location']  = array('like','%'.$re[1].'%');
        $maps['location'] = $location;
        $this->countPlant = M("plant")->where($map)->count();
        //充电桩总数
        $Char_all =M("char")->where(1)->select();
        $area = session("area");//当前运营商区号
        $strs = '';
        foreach($Char_all as $w){
            $stss = substr($w['key_link'],0,4);
            if(substr($stss,0,2) == 00 && substr($w['key_link'],1,3) == $area){
                $strs .= $w['key_link'].",";
            }elseif($stss == $area){
                $strs .= $w['key_link'] .",";
            }
        }
        $map['key_link'] = array('IN',rtrim($strs,','));
        $this->countChar = M("char")->where($map)->count();//电桩总数
        //充电总次数
        $this->countCharge  = floor((M("charge_total")->where($maps)->sum("total")) / 2);
        //预约总次数
        $this->countBespeak = floor((M("bespeak_total")->where($maps)->sum("total")) / 2);
        //账单总数
        $this->countBill    = M("bill_total")->where($maps)->sum("amount");
        $this->display();
    }
    //当前市下所有区县站点统计
    public function plant_city(){

        $re        = strsToArray(session('location'));
        $map['location'] = array('like','%'.$re[1].'%');
        $this->city  = M("plant")->where($map)->count();
        //分页
        $count = M("region")->where("pcode='".$re[1]."' && level=3")->count();
        vendor('Mcit.Sys.Page');
        $Page = new Page($count, 20);
        $pageView = $Page->show();
        $this->assign('pageView', $pageView); //发送分页
        $nowPage = isset($_GET['page']) ? $_GET['page'] : 1;
        $china = D("Admin/Region")->where("pcode='".$re[1]."' && level=3")->order("id")->page($nowPage . ',' . $Page->listRows)->select();
        //分页结束
        foreach($china as $k=>$v){
            $map['location'] = array('like','%'.$v['code'].'%');
            $china[$k]['num'] = M("plant")->where($map)->count();
        }
        $this->china= $china;
        $this->display();
    }
    //当前区县下详细
    public function district(){

        $map['location'] = array('like','%'.I("district").'%');
        $this->city = M("plant")->where($map)->count();
        $plant = M("plant")->where($map)->select();
        foreach($plant as $k=>$v){
            //将数字的地址转换成文字
            $region    = strsToArray($v['location']);
            $regionOne = D("Admin/Region")->where("code='".$region[2]."'")->field("fullname")->find();
            $plant[$k]['location'] = $regionOne['fullname'];
            if(mb_strlen($v['name'],'UTF8') <=17 ){
                $plant[$k]['name_text'] = $v['name'];
            }else{
                $plant[$k]['name_text'] = mb_substr($v['name'],0,17,"utf-8")."···";
            }
        }
        $this->plant = $plant;
        $this->display();
    }
    //当前平台下充电桩统计
    public function char_city(){

        $char_o = M("char")->where(1)->select();
        $area   = session("area");//当前运营商区号
        $strs   = '';
        foreach($char_o as $w){
            $stss = substr($w['key_link'],0,4);
            if(substr($stss,0,2) == 00 && substr($w['key_link'],1,3) == $area){
                $strs .= $w['key_link'].",";
            }elseif($stss == $area){
                $strs .= $w['key_link'] .",";
            }
        }
        //当前运商下所有电桩
        $map['key_link'] = array('IN',rtrim($strs,','));
        $this->city      = M("char")->where($map)->count();//电桩总数
        $re              = strsToArray(session('location'));
        $Map['location'] = array('like','%'.$re[1].'%');
        //分页
        $count = M("region")->where("pcode='".$re[1]."' && level=3")->count();
        vendor('Mcit.Sys.Page');
        $Page     = new Page($count, 20);
        $pageView = $Page->show();
        $this->assign('pageView', $pageView); //发送分页
        $nowPage  = isset($_GET['page']) ? $_GET['page'] : 1;
        $china    = D("Admin/Region")->where("pcode='".$re[1]."' && level=3")->order("id")->page($nowPage . ',' . $Page->listRows)->select();
        //分页结束
        foreach($china as $k=>$v){
            $location = $v['ppcode'].",".$v['pcode'].",".$v['code'];
            $map['location'] = array('like','%'.$location.'%');
            $plant           = M("plant")->where($Map)->select();
            $str             = '';
            foreach($plant as $s){
                if($s['location'] == $location){
                    $str    .= $s['id'].",";
                }
                $china[$k]['str']       = strsToArray($str);
                $china[$k]['plant_ids'] = rtrim($str,',');
            }
        }
        foreach($china as $h=>$d){
            $china[$h]['num'] = 0;
            for($i=0;$i<count($d['str']);$i++){
                $china[$h]['num'] += M("char")->where("ev_plant_id='".$d['str'][$i]."'")->count();
            }
        }
        $this->china= $china;
        $this->display();
    }

    //当前区域下电桩预览
    public function char_district(){

        $map['ev_plant_id'] = array('IN',I("code"));
        //分页
        $count = M("char")->where($map)->count();
        vendor('Mcit.Sys.Page');
        $Page     = new Page($count, 20);
        $pageView = $Page->show();
        $this->assign('pageView', $pageView); //发送分页
        $nowPage  = isset($_GET['page']) ? $_GET['page'] : 1;
        $char     = M("char")->where($map)->order("id")->page($nowPage . ',' . $Page->listRows)->select();
        //分页结束
        foreach($char as $k=>$v){
            //将数字的地址转换成文字
            $plant      = M("plant")->where("id='".$v['ev_plant_id']."'")->find();
            $region     = strsToArray($plant['location']);
            $regionOne  = D("Admin/Region")->where("code='".$region[2]."'")->field("fullname")->find();
            $char[$k]['region_text'] = $regionOne['fullname'];
            $char[$k]['plant_name']  = $plant['name'];
            if(mb_strlen($plant['name'],'UTF8') <=17 ){
                $char[$k]['plant_name_text'] = $plant['name'];
            }else{
                $char[$k]['plant_name_text'] = mb_substr($plant['name'],0,17,"utf-8")."···";
            }
        }
        $this->ev_char = $char;
        $this->city    = $count;
        $this->display();
    }

    //充电记录列表
    public function charge_list(){
        
        $charge_o = D("Admin/Charge")->where(1)->select();
        $area     = session("area");//当前运营商区号
        $strs     = '';
        foreach($charge_o as $w){
            $stss = substr($w['key_link'],0,4);
            if(substr($stss,0,2) == 00 && substr($w['key_link'],1,3) == $area){
                $strs .= $w['key_link'].",";
            }elseif($stss == $area){
                $strs .= $w['key_link'] .",";
            }
        }
        $map['key_link'] = array('IN',rtrim($strs,','));
        $start_time = I("start_time");
        if(!empty($_GET['end_time'])){
            $start_time  = date("Y-m-d H:s:i",strtotime($start_time));
            $end_time    = I("end_time");
            $end_time    = date("Y-m-d 23:59:59",strtotime($end_time));
            $map['time'] = array("between",array($start_time,$end_time));
        }
        if(!empty($_GET['start_time']) && $_GET['end_time'] == ''){
            $map['time'] = array("like","%".$start_time."%");
        }
        if(!empty($_GET['user_card'])){
            $map['user_card'] = I('user_card');
        }
        if(!empty($_GET['key_link'])){
            $map['key_link']  = I('key_link');
        }
        //分页
        $count = D("Admin/Charge")->where($map)->count();
        // 重搜一律回第一页
        if( $_GET['fsign'] == "abnormal" ){
            $_GET['fsign'] = "normal";
            $_GET['page']  = 1;
        }
        vendor('Mcit.Sys.Page');
        $Page     = new Page($count, 20);
        $pageView = $Page->show();
        $this->assign('pageView', $pageView); //发送分页
        $nowPage  = isset($_GET['page']) ? $_GET['page'] : 1;
        $charge   = D("Admin/Charge")->where($map)->order("time desc")->page($nowPage . ',' . $Page->listRows)->select();
        //分页结束
        foreach ($charge as $k => $v) {
            $char       = M("char")->where("key_link=".$v['key_link'])->find();
            $plant      = M("plant")->where("id=".$char['ev_plant_id'])->find();
            $arr        = strsToArray($plant['location']);
            $region     = M("region")->where("code=".$arr['2'])->find();
            $charge[$k]['region_text']   = $region['fullname'];
            $charge[$k]['plantAddr']     = $plant['name'];
            if(mb_strlen($plant['name'],"utf-8") <= 17){
                $charge[$k]['plantName'] = $plant['name'];
            }else{
                $charge[$k]['plantName'] = mb_substr($plant['name'],0,17,"utf-8")."···";
            }            
        }
        $this->charge = $charge;
        $this->display();
    }

    //详细查看页面
    public function charge_detail(){

        $charge = D("Admin/Charge")->where("id=".I("id"))->find();
        $char   = M("char")->where("key_link=".$charge['key_link'])->find();
        $plant  = M("plant")->where("id=".$char['ev_plant_id'])->find();
        $charge['evc_number'] = $char['evc_number'];
        $charge['name']       = $plant['name'];
        $charge['address']    = $plant['address'];
        $this->data           = $charge;    
        $this->display();
    }    

    //充电次数统计
    public function charge_statis(){

        $location  = session('location');
        $re        = strsToArray($location);
        $map['location']  = array('like','%'.$re[1].'%');
        $maps['location'] = $location;
        $this->sumCharge  = floor((M("charge_total")->where($maps)->sum("total")) / 2);
        if(!empty($_GET['end_time'])){
            $map['currentTime'] = array("between",array(I("start_time"),I("end_time")));
        }
        if(!empty($_GET['start_time']) && $_GET['end_time'] == ''){
            $map['currentTime'] = I("start_time");
        }
        if(!empty($_GET['month'])){
            $tomonth   = I("month");//某月
            $begin     = date("Y-m-01",strtotime($tomonth));//某月的第一天
            $end       = date('Y-m-d', strtotime("$begin +1 month -1 day"));//某月的最后一天
            $map['currentTime']  = array("between",array($begin,$end));
            $maps['currentTime'] = array("between",array($begin,$end));
            $this->sumCharge     = floor((M("charge_total")->where($maps)->sum("total")) / 2);
        }
        //分页
        $count = M("charge_total")->where($map)->count();
        // 重搜一律回第一页
        if( $_GET['fsign'] == "abnormal" ){
            $_GET['fsign'] = "normal";
            $_GET['page']  = 1;
        }
        vendor('Mcit.Sys.Page');
        $Page     = new Page($count, 20);
        $pageView = $Page->show();
        $this->assign('pageView', $pageView); //发送分页
        $nowPage  = isset($_GET['page']) ? $_GET['page'] : 1;
        $this->charge   = M("charge_total")->where($map)->order("currentTime desc")->page($nowPage . ',' . $Page->listRows)->select();
        //分页结束       
        $this->display();
    }

    //预约列表
    public function bespeak_list(){

        $bespeak_o = D("Admin/Bespeak")->where(1)->select();
        $area     = session("area");//当前运营商区号
        $strs     = '';
        foreach($bespeak_o as $w){
            $stss = substr($w['key_link'],0,4);
            if(substr($stss,0,2) == 00 && substr($w['key_link'],1,3) == $area){
                $strs .= $w['key_link'].",";
            }elseif($stss == $area){
                $strs .= $w['key_link'] .",";
            }
        }
        $map['key_link'] = array('IN',rtrim($strs,','));
        $start_time = I("start_time");
        if(!empty($_GET['end_time'])){
            $start_time  = date("Y-m-d H:s:i",strtotime($start_time));
            $end_time    = I("end_time");
            $end_time    = date("Y-m-d 23:59:59",strtotime($end_time));
            $map['time'] = array("between",array($start_time,$end_time));
        }
        if(!empty($_GET['start_time']) && $_GET['end_time'] == ''){
            $map['time'] = array("like","%".$start_time."%");
        }
        if(!empty($_GET['user_card'])){
            $map['user_card'] = I('user_card');
        }
        if(!empty($_GET['key_link'])){
            $map['key_link']  = I('key_link');
        }
        //分页
        $count = D("Admin/Bespeak")->where($map)->count();
        // 重搜一律回第一页
        if( $_GET['fsign'] == "abnormal" ){
            $_GET['fsign'] = "normal";
            $_GET['page']  = 1;
        }
        vendor('Mcit.Sys.Page');
        $Page     = new Page($count, 20);
        $pageView = $Page->show();
        $this->assign('pageView', $pageView); //发送分页
        $nowPage  = isset($_GET['page']) ? $_GET['page'] : 1;
        $bespeak   = D("Admin/Bespeak")->where($map)->order("time desc")->page($nowPage . ',' . $Page->listRows)->select();
        //分页结束
        foreach ($bespeak as $k => $v) {
            $char       = M("char")->where("key_link=".$v['key_link'])->find();
            $plant      = M("plant")->where("id=".$char['ev_plant_id'])->find();
            $arr        = strsToArray($plant['location']);
            $region     = M("region")->where("code=".$arr['2'])->find();
            $bespeak[$k]['region_text']   = $region['fullname'];
            $bespeak[$k]['plantAddr']     = $plant['name'];
            if(mb_strlen($plant['name'],"utf-8") <= 17){
                $bespeak[$k]['plantName'] = $plant['name'];
            }else{
                $bespeak[$k]['plantName'] = mb_substr($plant['name'],0,17,"utf-8")."···";
            }            
        }
        $this->bespeak = $bespeak;
        $this->display();
    }

    //预约详情
    public function bespeak_detail(){

        $bespeak = D("Admin/Bespeak")->where("id=".I("id"))->find();
        $char   = M("char")->where("key_link=".$bespeak['key_link'])->find();
        $plant  = M("plant")->where("id=".$char['ev_plant_id'])->find();
        $bespeak['evc_number'] = $char['evc_number'];
        $bespeak['name']       = $plant['name'];
        $bespeak['address']    = $plant['address'];
        $this->data           = $bespeak;
        $this->display();
    }
    //预约统计
    public function bespeak_statis(){

        $location = session('location');
        $re       = strsToArray($location);
        $map['location']  = array('like','%'.$re[1].'%');
        $maps['location'] = $location;
        $this->sumBespeak = floor((M("bespeak_total")->where($maps)->sum("total")) / 2);
        if(!empty($_GET['end_time'])){
            $map['currentTime'] = array("between",array(I("start_time"),I("end_time")));
        }
        if(!empty($_GET['start_time']) && $_GET['end_time'] == ''){
            $map['currentTime'] = I("start_time");
        }
        if(!empty($_GET['month'])){
            $tomonth   = I("month");//某月
            $begin     = date("Y-m-01",strtotime($tomonth));//某月的第一天
            $end       = date('Y-m-d', strtotime("$begin +1 month -1 day"));//某月的最后一天
            $map['currentTime']  = array("between",array($begin,$end));
            $maps['currentTime'] = array("between",array($begin,$end));
            $this->sumBespeak    = floor((M("bespeak_total")->where($maps)->sum("total")) / 2);
        }
        //分页
        $count = M("bespeak_total")->where($map)->count();
        // 重搜一律回第一页
        if( $_GET['fsign'] == "abnormal" ){
            $_GET['fsign'] = "normal";
            $_GET['page']  = 1;
        }
        vendor('Mcit.Sys.Page');
        $Page     = new Page($count, 20);
        $pageView = $Page->show();
        $this->assign('pageView', $pageView); //发送分页
        $nowPage  = isset($_GET['page']) ? $_GET['page'] : 1;
        $this->bespeak   = M("bespeak_total")->where($map)->order("currentTime desc")->page($nowPage . ',' . $Page->listRows)->select();
        //分页结束 
        $this->display();
    }
    //账单统计
    public function bill_statis(){

        $location = session('location');
        $re       = strsToArray($location);
        $map['location']  = array('like','%'.$re[1].'%');
        $maps['location'] = $location;
        $this->sumBill    = M("bill_total")->where($maps)->sum("amount");//总金额
        $this->countBill  = M("bill_total")->where($maps)->sum("total");//总计
        if(!empty($_GET['end_time'])){
            $map['currentTime'] = array("between",array(I("start_time"),I("end_time")));
        }
        if(!empty($_GET['start_time']) && $_GET['end_time'] == ''){
            $map['currentTime'] = I("start_time");
        }
        if(!empty($_GET['month'])){
            $tomonth   = I("month");//某月
            $begin     = date("Y-m-01",strtotime($tomonth));//某月的第一天
            $end       = date('Y-m-d', strtotime("$begin +1 month -1 day"));//某月的最后一天
            $map['currentTime']  = array("between",array($begin,$end));
            $maps['currentTime'] = array("between",array($begin,$end));
            $this->sumBill    = M("bill_total")->where($maps)->sum("amount");//总金额
            $this->countBill  = M("bill_total")->where($maps)->sum("total");//总计
        }
        //分页
        $count = M("bill_total")->where($map)->count();
        // 重搜一律回第一页
        if( $_GET['fsign'] == "abnormal" ){
            $_GET['fsign'] = "normal";
            $_GET['page']  = 1;
        }
        vendor('Mcit.Sys.Page');
        $Page     = new Page($count, 20);
        $pageView = $Page->show();
        $this->assign('pageView', $pageView); //发送分页
        $nowPage  = isset($_GET['page']) ? $_GET['page'] : 1;
        $this->bill   = M("bill_total")->where($map)->order("currentTime desc")->page($nowPage . ',' . $Page->listRows)->select();
        //分页结束
        $this->display();
    }
    //设备变化记录列表
    public function change_list(){

        $this->display();
    }
    //设备变化记录统计
    public function change_statis(){

        $this->display();
    }
    //设备注册记录列表
    public function regist_list(){

        $this->display();
    }
    //设备注册记录统计
    public function regist_statis(){

        $this->display();
    }
    //刷卡记录列表
    public function off_card_list(){

        $this->display();
    }
    //刷卡记录统计
    public function off_card_statis(){

        $this->display();
    }


}